import { useModel } from "umi";
import { useReactive } from "ahooks";
import { useEffect } from "react";

import { AllOrganizationApplicationAuthorizeAppliesByWait } from "../_graphql/customer";

const useWaitExamineAuthorize = () => {
    const { graphqlFetchPlatform, remoteData } = useModel("global");

    const state = useReactive({
        dataLoading: false,
        waitExamineApply: [],
    });

    const loadWaitExamineApply = async () => {
        state.dataLoading = true;
        let res = await graphqlFetchPlatform({
            query: AllOrganizationApplicationAuthorizeAppliesByWait,
            variables: {},
        });
        state.waitExamineApply = res?.a?.nodes || [];
        state.dataLoading = false;
        return res?.a?.nodes || [];
    };

    const updateExamineRecord = async (oaaaUuid, exInfo, oaaaExst, callback) => {
        state.dataLoading = true;
        let query = `mutation MyMutation($oaaaUuid: UUID!, $exInfo: JSON!, $oaaaExst: String!) {
            updateOrganizationApplicationAuthorizeApplyByOaaaUuid(
              input: {
                organizationApplicationAuthorizeApplyPatch: { exInfo: $exInfo, oaaaExst: $oaaaExst }
                oaaaUuid: $oaaaUuid
              }
            ) { clientMutationId }
        }`;
        let res = await graphqlFetchPlatform({ query, variables: { oaaaUuid, exInfo, oaaaExst } });
        res && callback && callback(res);
        state.dataLoading = false;
    };

    const agreeAuthorizeApply = async (params, callback) => {
        state.dataLoading = true;
        let res = await remoteData({
            object: "bnt-application-operation-center-service",
            serviceName: "/operator/organization/customer/agreeAuthorizeApply",
            params,
        });
        res && callback && callback(res);
        state.dataLoading = false;
    };

    const updateRealityPrice = async (variables, callback) => {
        state.dataLoading = true;
        let query = `mutation MyMutation($oaaaUuid: UUID!, $sdRepr: BigFloat, $spRepr: BigFloat ) {
            updateOrganizationApplicationAuthorizeApplyByOaaaUuid(
              input: {
                organizationApplicationAuthorizeApplyPatch: { sdRepr: $sdRepr spRepr: $spRepr }
                oaaaUuid: $oaaaUuid
              }
            ) { clientMutationId }
        }`;
        let res = await graphqlFetchPlatform({ query, variables });
        res && callback && callback(res);
        state.dataLoading = false;
    };

    return { ...state, loadWaitExamineApply, updateExamineRecord, updateRealityPrice, agreeAuthorizeApply };
};

export default useWaitExamineAuthorize;
